package com.example.unisys.controller;


import com.example.unisys.cache.UserManager;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.User;
import com.example.unisys.service.*;
import com.example.unisys.utils.IpUtil;
import com.example.unisys.utils.JWTUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping(value = "/apis/v1/dash", produces = "application/json; charset=utf-8")
public class DashController {

    @Autowired
    StudentService studentService;

    @Autowired
    CourseService courseService;

    @Autowired
    SchoolService schoolService;

    @Autowired
    UserService userService;

    @Autowired
    CourseSaleService courseSaleService;

    @Autowired
    MainCourseService maincourseService;

    @Autowired
    SubCourseService subCourseService;

    @Autowired
    UserManager userManager;

    /*
     * 获取看板数据展示
     * 需要根据不同用户角色
     * */
//    @RequiresPermissions("system:dash")
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public Response getDashData(@CookieValue("Admin-Token") String token, HttpServletRequest request) {
        HashMap<String, Object> dashObject = new HashMap<>();
        String username = JWTUtil.getUsernameByDecrypt(token);
        User user=userManager.getUser(username);
        String ip = IpUtil.getIpAddr(request);
        //cache更新并未写回
        userService.updateip(ip, user.getUser_id());


        //插入基本信息
        dashObject.put("cur_ip", ip);
        dashObject.put("prev_ip", user.getIp());
        dashObject.put("prev_time", user.getPrev_time());
        dashObject.put("admin_type", user.getUser_type());
        if (user.getUser_type() == 1) {
            System.out.println("系统管理员");
            dashObject.put("student_num", studentService.getAllStuNum());
            dashObject.put("course_num", courseService.getRepoCourseSum());
            dashObject.put("school_num", schoolService.getAllSchoolSum());
            dashObject.put("sales", courseSaleService.getAll());

            HashMap<String, Object> hs = new HashMap<>();
            hs.put("start", 0);
            hs.put("pagesize", 10);
            dashObject.put("courses", courseSaleService.getSalesBySystem(hs));

        } else if (user.getUser_type() == 2) {
            System.out.println("主校管理员");
            dashObject.put("student_num", studentService.getMainStuNum(user.getMain_school()));
            dashObject.put("course_num", maincourseService.getAllCourse(user.getMain_school()));
            dashObject.put("school_num", schoolService.getSchoolByMain(user.getMain_school()));
            dashObject.put("sales", courseSaleService.getAllByMain(user.getMain_school()));


            HashMap<String, Object> hs = new HashMap<>();
            hs.put("main_school", user.getMain_school());
            hs.put("start", 0);
            hs.put("pagesize", 10);
            dashObject.put("courses", courseSaleService.getSalesByMain(hs));
        } else if (user.getUser_type() == 3) {
            System.out.println("分校管理员");
            dashObject.put("student_num", studentService.getStuNum(user.getMain_school(), user.getSub_school()));
            dashObject.put("course_num", subCourseService.getCourseSum(user.getMain_school(), user.getSub_school()));
            dashObject.put("school_num", 1);
            dashObject.put("sales", courseSaleService.getAllBySub(user.getMain_school(), user.getSub_school()));

            HashMap<String, Object> hs = new HashMap<>();
            hs.put("main_school", user.getMain_school());
            hs.put("sub_school", user.getSub_school());
            hs.put("start", 0);
            hs.put("pagesize", 10);
            dashObject.put("courses", courseSaleService.getSalesBySub(hs));
        } else {

            return Response.builder().status(STATUS.BAD_REQUEST).build();
        }

        return Response.builder().status(STATUS.SUCCESS).data(dashObject).build();
    }
}
